require 'wpspider/importer/wppage_vo'

module WPSpider
  module Importer

    #
    # XMLParser class.
    #
    class XMLParser

      #
      # Constructor.
      # - xml_file: XML file name.
      #
      def initialize(xml_file)
        @xml_file = xml_file
      end

      #
      # Parses XML.
      #
      def parse()
        title = ''
        text = ''
        intitle = false
        intext = false
        File::open(@xml_file, 'r') do |io|
          io.each() do |line|
            if(line =~ /<title/) then
              intitle = true
            end
            if(intitle) then
              title += strip(line)
            end
            if(line =~ /<\/title/) then
              intitle = false
              if(title.include?(':'))
              title = '' 
              end
            end
            if(line =~ /<text/ && !title.empty?()) then
              intext = true
            end
            if(intext) then
              text += strip(line)
            end
            if(line =~ /<\/text/ && !title.empty?()) then
              begin
                intext = false
                words = get_words(text)
                wppage_vo = WPPageVO.new(title, text.size, words)
                yield(wppage_vo)
              ensure
                title = ''
                text = ''
              end
            end
          end
        end
      end

      #
      # Removes element tokens <...>.
      # - str: Input string.
      # - return: Output string without element tokens.
      #
      def strip(str)
        return str.gsub(/<[^>]*>/, '').strip()
      end
      private :strip

      #
      # Gets words surrounded by '[[' and ']]'.
      # - str: Input string.
      # - return: Array of words
      #
      def get_words(str)
        words = str.scan(/\[\[([^\]\|]+)(?:|[^\]]*)\]\]/).flatten()
        #words = str.scan(/\[\[([^\]]+)\]\]/).flatten()
        words.reject!() do |word| 
          word.include?(':') || \
          60 < word.length
        end
        return words.uniq
      end
      private :get_words
    end
  end
end
